using UnityEngine;

public class SimpleRuntimeEditor : MonoBehaviour
{
	private enum ControlType
	{
		Point = 0,
		InCtrl = 1,
		OutCtrl = 2,
		NotSelected = 3
	}

	private RageSpline activeControl;

	private int activeControlPointIndex = -1;

	private ControlType activeControlType = ControlType.NotSelected;

	private IRageSpline rageSpline;

	public GameObject controlPointPrefab;

	public GameObject handleControlPointPrefab;

	public GameObject handleLinePrefab;

	public Color selectedColor = new Color(1f, 1f, 0.4f);

	public Color unSelectedColor = new Color(1f, 1f, 1f);

	public float maxSelectionRange = 2f;

	private RageSpline[] controlPoints;

	private RageSpline[] inControlPoints;

	private RageSpline[] outControlPoints;

	private RageSpline[] handleLines;

	private void Awake()
	{
		rageSpline = GetComponent(typeof(RageSpline)) as IRageSpline;
	}

	private void Start()
	{
		controlPoints = new RageSpline[rageSpline.GetPointCount()];
		inControlPoints = new RageSpline[rageSpline.GetPointCount()];
		outControlPoints = new RageSpline[rageSpline.GetPointCount()];
		handleLines = new RageSpline[rageSpline.GetPointCount()];
		for (int i = 0; i < rageSpline.GetPointCount(); i++)
		{
			GameObject gameObject = Object.Instantiate(controlPointPrefab, rageSpline.GetPositionWorldSpace(i) + new Vector3(0f, 0f, -1f), Quaternion.identity);
			controlPoints[i] = gameObject.GetComponent(typeof(RageSpline)) as RageSpline;
			controlPoints[i].SetFillColor1(unSelectedColor);
			GameObject gameObject2 = Object.Instantiate(handleControlPointPrefab, rageSpline.GetInControlPositionWorldSpace(i) + new Vector3(0f, 0f, -1f), Quaternion.identity);
			inControlPoints[i] = gameObject2.GetComponent(typeof(RageSpline)) as RageSpline;
			inControlPoints[i].SetFillColor1(unSelectedColor);
			GameObject gameObject3 = Object.Instantiate(handleControlPointPrefab, rageSpline.GetOutControlPositionWorldSpace(i) + new Vector3(0f, 0f, -1f), Quaternion.identity);
			outControlPoints[i] = gameObject3.GetComponent(typeof(RageSpline)) as RageSpline;
			outControlPoints[i].SetFillColor1(unSelectedColor);
			GameObject gameObject4 = Object.Instantiate(handleLinePrefab, rageSpline.GetPositionWorldSpace(i) + new Vector3(0f, 0f, -0.5f), Quaternion.identity);
			handleLines[i] = gameObject4.GetComponent(typeof(RageSpline)) as RageSpline;
		}
		refreshEditorGUI();
	}

	private void Update()
	{
		Vector3 vector = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		vector = new Vector3(vector.x, vector.y, 0f);
		if (Input.GetMouseButtonDown(0))
		{
			selectNearestItem(vector);
		}
		if (Input.GetMouseButton(0))
		{
			updateActiveControlPosition(vector);
			refreshEditorGUI();
			rageSpline.RefreshMesh();
		}
	}

	public void refreshEditorGUI()
	{
		for (int i = 0; i < rageSpline.GetPointCount(); i++)
		{
			controlPoints[i].transform.position = rageSpline.GetPositionWorldSpace(i) + new Vector3(0f, 0f, -1f);
			inControlPoints[i].transform.position = rageSpline.GetInControlPositionWorldSpace(i) + new Vector3(0f, 0f, -1f);
			outControlPoints[i].transform.position = rageSpline.GetOutControlPositionWorldSpace(i) + new Vector3(0f, 0f, -1f);
			handleLines[i].transform.position = rageSpline.GetPositionWorldSpace(i) + new Vector3(0f, 0f, -0.5f);
			handleLines[i].SetPointWorldSpace(0, rageSpline.GetInControlPositionWorldSpace(i));
			handleLines[i].SetPointWorldSpace(1, rageSpline.GetOutControlPositionWorldSpace(i));
			handleLines[i].SetOutControlPositionPointSpace(0, (rageSpline.GetPosition(i) - rageSpline.GetOutControlPosition(i)) * 0.25f);
			handleLines[i].SetOutControlPositionPointSpace(1, (rageSpline.GetInControlPosition(i) - rageSpline.GetPosition(i)) * 0.25f);
			handleLines[i].RefreshMesh(false, true, false);
		}
	}

	public void updateActiveControlPosition(Vector3 position)
	{
		switch (activeControlType)
		{
		case ControlType.Point:
			rageSpline.SetPointWorldSpace(activeControlPointIndex, position);
			break;
		case ControlType.InCtrl:
			rageSpline.SetInControlPositionWorldSpace(activeControlPointIndex, position);
			break;
		case ControlType.OutCtrl:
			rageSpline.SetOutControlPositionWorldSpace(activeControlPointIndex, position);
			break;
		}
	}

	public void selectNearestItem(Vector3 position)
	{
		float num = 1E+09f;
		RageSpline rageSpline = activeControl;
		for (int i = 0; i < this.rageSpline.GetPointCount(); i++)
		{
			if ((controlPoints[i].transform.position - position).magnitude < num)
			{
				num = (controlPoints[i].transform.position - position).magnitude;
				activeControlPointIndex = i;
				activeControlType = ControlType.Point;
				activeControl = controlPoints[i];
			}
			if ((inControlPoints[i].transform.position - position).magnitude < num)
			{
				num = (inControlPoints[i].transform.position - position).magnitude;
				activeControlPointIndex = i;
				activeControlType = ControlType.InCtrl;
				activeControl = inControlPoints[i];
			}
			if ((outControlPoints[i].transform.position - position).magnitude < num)
			{
				num = (outControlPoints[i].transform.position - position).magnitude;
				activeControlPointIndex = i;
				activeControlType = ControlType.OutCtrl;
				activeControl = outControlPoints[i];
			}
		}
		if (num > maxSelectionRange)
		{
			activeControl = null;
			activeControlPointIndex = -1;
			activeControlType = ControlType.NotSelected;
		}
		if (rageSpline != activeControl)
		{
			if (rageSpline != null)
			{
				rageSpline.SetFillColor1(unSelectedColor);
				rageSpline.RefreshMesh();
			}
			if (activeControl != null)
			{
				activeControl.SetFillColor1(selectedColor);
				activeControl.RefreshMesh();
			}
		}
	}
}
